package com.ryan.route.controller;


import com.ryan.route.entity.RouteCollect;
import com.ryan.route.entity.frontvo.RouteCollectVo;
import com.ryan.route.service.RouteCollectService;
import com.ryan.utils.JwtUtil;
import com.ryan.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 路线收藏 前端控制器
 * </p>
 *
 * @author ryan
 * @since 2020-08-03
 */
@RestController
@RequestMapping("/route/collect")
public class RouteCollectController {

    @Autowired
    private RouteCollectService collectService;

    /**
     * 判断是否已经收藏
     * @param routeId
     * @return
     */
    @GetMapping("/isCollect/{routeId}/{token}")
    public R isCollect(@PathVariable String routeId, HttpServletRequest request){
        String memberId = JwtUtil.getMemberIdByJwtToken(request);
        if (!StringUtils.isEmpty(memberId)){
            boolean isCollect = collectService.isCollect(routeId, memberId);
            return R.ok().data("isCollect",isCollect);
        }
        return R.ok().data("isCollect",false);
    }

    /**
     * 添加收藏
     * @param routeCollect
     * @return
     */
    @PostMapping("/collect")
    public R collect(@RequestBody RouteCollect routeCollect){
        boolean save = collectService.save(routeCollect);
        return save?R.ok():R.error();
    }

    /**
     * 移除收藏
     * @param routeId
     * @param request
     * @return
     */
    @DeleteMapping("/removeCollect/{routeId}")
    public R removeCollect(@PathVariable String routeId, HttpServletRequest request){
        String memberId = JwtUtil.getMemberIdByJwtToken(request);
        int count = collectService.removeCollect(routeId,memberId);
        return count>0?R.ok():R.error();
    }

    /**
     * 根据会员id，获取收藏列表
     * @return
     */
    @GetMapping("/getCollectList")
    public R getCollectList(HttpServletRequest request){
        List<RouteCollectVo> list = collectService.getCollectList(JwtUtil.getMemberIdByJwtToken(request));
        return R.ok().data("list",list);
    }
}

